package it.aekidna.xmlmapping
{
   import flash.events.EventDispatcher;
   
   import mx.core.ClassFactory;
   import mx.core.IFactory;
   import mx.events.PropertyChangeEvent;
   import mx.formatters.Formatter;
   
   [DefaultProperty("mapping")]
   [Event(name="beforePropertyMap", type="it.aekidna.xmlmapping.XmlMapPropertyEvent")]
   [Event(name="parse", type="it.aekidna.xmlmapping.XmlMapPropertyParseEvent")]
   public class MapProperty extends EventDispatcher
   {
      XMLMAPPING_PRIVATE var _parentMapping : XmlMapping;
      
      /**
      * property name of the object being created.
      */
      public var targetProperty : String;
      
      /**
      * XPath expression pointing to the xml node to be evaluated
      */
      public var source : String = ".";
      
      /**
      * if false, no exception will be thrown when the source expression 
      * cannot be resolved. 
      * An exception is thrown if destination property does not exist. 
      */
      public var mandatory : Boolean = true;
      
      /**
      * an IXmlMappingParser class to be used for custom parsing of complex objects such as Date.
      * 
      * 
      * If parser is the current mapping's class object, the mapping object will be used as a parser. 
      * This assumes that the mapping object implements IXmlMappingParser
      */
      public var parserClass : IFactory = null;
      
      /**
      * a dictionary of parameters to be used by the parser:
      * for instance, a date parser might need an expected format string
      */
      public var parserContext : Object;
      /**
      * a formatter object to be used to format parsed data in a target property of type String.
      */ 
      public var formatter : Formatter;
      
      /**
      * specifies the target property's type. 
      * 
      * Used to perform mappings against untyped objects ( Flash's Object ). Note that in this scenario,
      * arrays are automatically created for source expressions resolving to multiple nodes.
      * If not defined, this targetProperty is mapped as String.
      * Ignored elsewhere. 
      */ 
      public var targetPropertyClass : IFactory = new ClassFactory( String );
      
      public var targetClass : IFactory
      
      
      /**
      * specifies the XmlMapping class or class instance to be used to map custom classes objects, 
      * Arrays and ArrayCollections
      */
      public var mapping : Object;
      
      public var version : String = "*";
      
      public function get value( ) : *
      {
         return XMLMAPPING_PRIVATE::_parentMapping.context.getPropertyValue( this );
      }
      
      public function set value( inValue : Object ) : void
      {
   	   dispatchEvent( new PropertyChangeEvent( PropertyChangeEvent.PROPERTY_CHANGE, false, false, null, "value", null, inValue ) );
      }
   }
}